package com.android.baseconfig.common.views;

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.baseconfig.R;
import com.android.baseconfig.common.utils.SystemUtils;

import androidx.annotation.DrawableRes;
import androidx.annotation.StringRes;

/**
 *
 * @author Angzhi Li
 * @date 2017/6/13
 * app:title_text 标题
 * app:left_drawable 左边图片
 * app:right_drawable 右边图片
 * app:right_text 右边文字
 */
public class CustomTitleBar extends FrameLayout {
    ImageView ivLeft;
    TextView tvLeft;
    TextView tvTitle;
    ImageView ivCenter;
    TextView tvRight;
    ImageView ivFirstRightTip;
    ImageView ivRightTip;
    View firstViewRight;
    View viewRight;
    ImageView ivRight;
    ImageView ivFirstRight;
    BadgeView bvCount;
    View bottomLine;

    public CustomTitleBar(Context context) {
        this(context, null);
    }

    public CustomTitleBar(Context context, AttributeSet attrs) {
        this(context, attrs, -1);
    }

    public CustomTitleBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomTitleBar);
        CharSequence mytoolbar_title = typedArray.getText(R.styleable.CustomTitleBar_title_text);
        CharSequence mytoolbar_right_text = typedArray.getText(R.styleable.CustomTitleBar_right_text);
        CharSequence mytoolbar_left_text = typedArray.getText(R.styleable.CustomTitleBar_left_text);
        int mytoolbar_right_tips_drawable = typedArray.getResourceId(R.styleable.CustomTitleBar_right_tips_drawable, -1);
        int mytoolbar_first_right_tips_drawable = typedArray.getResourceId(R.styleable.CustomTitleBar_first_right_tips_drawable, -1);
        int mytoolbar_right_drawable = typedArray.getResourceId(R.styleable.CustomTitleBar_right_drawable, -1);
        int mytoolbar_first_right_drawable = typedArray.getResourceId(R.styleable.CustomTitleBar_first_right_drawable, -1);
        int mytoolbar_left_drawable = typedArray.getResourceId(R.styleable.CustomTitleBar_left_drawable, R.drawable.top_icon_leftbutton_back_black);
        int mytoolbar_title_color = typedArray.getColor(R.styleable.CustomTitleBar_title_color, context.getResources().getColor(R.color.content_default));
        int mytoolbar_left_text_color = typedArray.getColor(R.styleable.CustomTitleBar_text_color, context.getResources().getColor(R.color.content_default));
        int mytoolbar_right_text_color = typedArray.getColor(R.styleable.CustomTitleBar_right_text_color, context.getResources().getColor(R.color.content_default));
        // ColorStateList mytoolbar_text_colors = typedArray.getColorStateList(R.styleable.mytoolbar_text_color);
        int mytoolbar_bg_resource = typedArray.getResourceId(R.styleable.CustomTitleBar_bg_resource, R.color.white);
        float titleTextSize = typedArray.getDimensionPixelSize(R.styleable.CustomTitleBar_title_text_size, context.getResources().getDimensionPixelSize(R.dimen.sp_18));
        float rightTextSize = typedArray.getDimensionPixelSize(R.styleable.CustomTitleBar_right_text_size, context.getResources().getDimensionPixelSize(R.dimen.sp_17));
        float leftTextSize = typedArray.getDimensionPixelSize(R.styleable.CustomTitleBar_left_text_size, context.getResources().getDimensionPixelSize(R.dimen.sp_18));
        int translucent = typedArray.getInteger(R.styleable.CustomTitleBar_translucent, 0);
        boolean lineVisible = typedArray.getBoolean(R.styleable.CustomTitleBar_bottom_line_visible, true);

        initView(translucent);

        // tvTitle和tvLeft互换了
        if (!TextUtils.isEmpty(mytoolbar_title)) {
            mytoolbar_left_text = mytoolbar_title;
        }
        mytoolbar_left_text_color = mytoolbar_title_color;
        setTitle(mytoolbar_title);
        setTitleColor(mytoolbar_title_color);
        setTextColor(mytoolbar_left_text_color);
        setRightTextColor(mytoolbar_right_text_color);
        setTitleTextSize(titleTextSize);
        setRightText(mytoolbar_right_text);
        setLeftText(mytoolbar_left_text);
        setRightTextSize(rightTextSize);
        setLeftTextSize(leftTextSize);
        setBottomLineVisible(lineVisible);
        if (mytoolbar_right_tips_drawable != -1) {
            setRightTipsIcon(mytoolbar_right_tips_drawable);
        }
        if (mytoolbar_right_drawable != -1) {
            setRightIcon(mytoolbar_right_drawable);
        }
        if (mytoolbar_first_right_drawable != -1) {
            setFirstRightIcon(mytoolbar_first_right_drawable);
        }
        if (mytoolbar_left_drawable != -1) {
            setLeftIcon(mytoolbar_left_drawable);
            // tvTitle和tvLeft互换了
            // if (getTvLeft().getVisibility() == View.VISIBLE) {
            //     ViewGroup.MarginLayoutParams p = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
            //     p.setMargins(-24, 0, 0, 0);
            //     getTvLeft().setLayoutParams(p);
            //     getTvLeft().setGravity(Gravity.CENTER_VERTICAL);
            // }
        }
        setBackgroundResource(mytoolbar_bg_resource);
    }

    private void initView(int translucent) {
        View.inflate(getContext(), R.layout.layout_toolbar, this);
        tvLeft = (TextView) findViewById(R.id.tv_left);
        ivLeft = (ImageView) findViewById(R.id.iv_left);
        tvTitle = (TextView) findViewById(R.id.tv_title);
        ivCenter = (ImageView) findViewById(R.id.iv_center);
        tvRight = (TextView) findViewById(R.id.tv_right);
        ivFirstRightTip = (ImageView) findViewById(R.id.iv_first_right_tip);
        ivRightTip = (ImageView) findViewById(R.id.iv_right_tip);
        firstViewRight = findViewById(R.id.first_view_right);
        viewRight = findViewById(R.id.view_right);
        ivRight = (ImageView) findViewById(R.id.iv_right);
        ivFirstRight = (ImageView) findViewById(R.id.iv_first_right);
        bvCount = (BadgeView) findViewById(R.id.bd_unread_message_num);
        bvCount.setVisibility(View.GONE);
        bottomLine = findViewById(R.id.bottom_line);
        if (translucent == 0) {
            int statusBarHeight = SystemUtils.getDefaultStatusHeight(getContext());
            setPadding(getPaddingLeft(), statusBarHeight, getPaddingRight(), getPaddingBottom());
        }
    }

    public ImageView getIvLeft() {
        return ivLeft;
    }

    public TextView getTvTitle() {
        return tvTitle;
        // return tvLeft;
    }

    public ImageView getIvCenter() {
        return ivCenter;
    }

    public TextView getTvRight() {
        return tvRight;
    }

    public TextView getTvLeft() {
        // return tvLeft;
        return tvTitle;
    }

    public ImageView getIvFirstRightTip() {
        return ivFirstRightTip;
    }

    public ImageView getIvRightTip() {
        return ivRightTip;
    }

    public ImageView getIvRight() {
        return ivRight;
    }

    public ImageView getIvFirstRight() {
        return ivFirstRight;
    }

    public View getViewFirstRight() {
        return firstViewRight;
    }

    public View getViewRight() {
        return viewRight;
    }

    public BadgeView getBvCount() {
        return bvCount;
    }

    public void setBvCount(int count) {
        if (count > 0) {
            bvCount.setVisibility(View.VISIBLE);
            bvCount.setText(count + "");
            if (count > 99) {
                bvCount.setText("99+");
            }
        } else {
            bvCount.setVisibility(View.GONE);
        }
    }

    public void setTitle(@StringRes int resId) {
        tvTitle.setText(resId);
        tvTitle.setVisibility(View.VISIBLE);
        // tvLeft.setText(resId);
    }

    public void setTitle(CharSequence title) {
        tvTitle.setText(title);
        tvTitle.setVisibility(View.VISIBLE);
        // tvLeft.setText(title);
    }

    public void setRightText(@StringRes int resId) {
        String rightText = getContext().getString(resId);
        if (!TextUtils.isEmpty(rightText)) {
            tvRight.setText(rightText);
            tvRight.setVisibility(View.VISIBLE);
        } else {
            tvRight.setText("");
            tvRight.setVisibility(View.GONE);
        }
    }

    public void setRightText(CharSequence rightText) {
        if (!TextUtils.isEmpty(rightText)) {
            tvRight.setText(rightText);
            tvRight.setVisibility(View.VISIBLE);
            firstViewRight.setVisibility(GONE);
            viewRight.setVisibility(GONE);
        } else {
            tvRight.setText("");
            tvRight.setVisibility(View.GONE);
        }
    }

    public void setLeftText(CharSequence leftText) {
        if (!TextUtils.isEmpty(leftText)) {
            // tvLeft.setText(leftText);
            // tvLeft.setVisibility(View.VISIBLE);
            tvTitle.setText(leftText);
            tvTitle.setVisibility(View.VISIBLE);
        } else {
            // tvLeft.setText("");
            // tvLeft.setVisibility(View.GONE);
            tvTitle.setText("");
            tvTitle.setVisibility(View.GONE);
        }
    }

    public void setLeftIcon(@DrawableRes int resId) {
        ivLeft.setVisibility(View.VISIBLE);
        ivLeft.setImageResource(resId);
    }

    public void setFirstRightTipsIcon(@DrawableRes int resId) {
        ivFirstRightTip.setVisibility(View.VISIBLE);
        ivFirstRightTip.setImageResource(resId);
    }

    public void setRightTipsIcon(@DrawableRes int resId) {
        ivRightTip.setVisibility(View.VISIBLE);
        ivRightTip.setImageResource(resId);
    }

    public void setRightIcon(@DrawableRes int resId) {
        ivRight.setVisibility(View.VISIBLE);
        ivRight.setImageResource(resId);
    }

    public void setCenterIcon(@DrawableRes int resId) {
        ivCenter.setVisibility(View.VISIBLE);
        ivCenter.setImageResource(resId);
    }

    public void setFirstRightIcon(@DrawableRes int resId) {
        ivFirstRight.setVisibility(View.VISIBLE);
        ivFirstRight.setImageResource(resId);
    }

    public void setViewRightOnClickListener(OnClickListener listener) {
        viewRight.setOnClickListener(listener);
    }

    public void setViewFirstRightOnClickListener(OnClickListener listener) {
        firstViewRight.setOnClickListener(listener);
    }

    public void setIvRightOnClickListener(OnClickListener listener) {
        ivRight.setOnClickListener(listener);
    }

    public void setIvFirstRightOnClickListener(OnClickListener listener) {
        ivFirstRight.setOnClickListener(listener);
    }

    public void setIvLeftOnClickListener(OnClickListener listener) {
        ivLeft.setOnClickListener(listener);
    }

    public void setIvCenterOnClickListener(OnClickListener listener) {
        ivCenter.setOnClickListener(listener);
    }

    public void setTvRightOnClickListener(OnClickListener listener) {
        tvRight.setOnClickListener(listener);
    }

    public void setTvLeftOnClickListener(OnClickListener listener) {
        // tvLeft.setOnClickListener(listener);
    }

    public void setTitleTextSize(float titleTextSize) {
        // tvTitle.setTextSize(TypedValue.COMPLEX_UNIT_PX, titleTextSize);
        tvLeft.setTextSize(TypedValue.COMPLEX_UNIT_PX, titleTextSize);
    }

    public void setRightTextSize(float titleTextSize) {
        tvRight.setTextSize(TypedValue.COMPLEX_UNIT_PX, titleTextSize);
    }

    public void setLeftTextSize(float titleTextSize) {
        // tvLeft.setTextSize(TypedValue.COMPLEX_UNIT_PX, titleTextSize);
        tvTitle.setTextSize(TypedValue.COMPLEX_UNIT_PX, titleTextSize);
    }

    public void setBottomLineVisible(boolean visible) {
        if (bottomLine != null) {
            bottomLine.setVisibility(visible ? VISIBLE : GONE);
        }
    }

    public void setTitleColor(int titleColor) {
        tvTitle.setTextColor(titleColor);
        // tvLeft.setTextColor(titleColor);
    }

    public void setTextColor(int textColor) {
        // tvLeft.setTextColor(textColor);
        tvTitle.setTextColor(textColor);
    }

    public void setTextColor(ColorStateList textColors) {
        // tvLeft.setTextColor(textColors);
        tvTitle.setTextColor(textColors);
    }

    public void setRightTextColor(int color) {
        tvRight.setTextColor(color);
    }
}
